package com.buu.system.controller;

import com.buu.common.annotation.Log;
import com.buu.common.controller.BaseController;
import com.buu.common.domain.Tree;
import com.buu.common.utils.MD5Utils;
import com.buu.common.utils.R;
import com.buu.common.utils.ShiroUtils;
import com.buu.manager.domain.EduDO;
import com.buu.manager.service.EduService;
import com.buu.system.domain.MenuDO;
import com.buu.system.domain.SysUserDO;
import com.buu.system.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LoginController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	MenuService menuService;

	@Autowired
	EduService eduService;

	@GetMapping({ "/", "", "/index" })
	String Index(Model model) {
		//Tree<MenuDO> menuTree = menuService.getSysMenuTree(getUserId());
		List<Tree<MenuDO>> menus = menuService.listMenuTree(getUserId());
		model.addAttribute("menus", menus);
		model.addAttribute("name", getUser().getName());
		logger.info(getUser().getName());
		return "index";
	}

	@GetMapping("/registry")
	String registry(Model model) {

		return "registry";
	}

	@GetMapping("/login")
	String login() {
		return "login";
	}

	@PostMapping("/login_bak")
	String doLogin(String username, String password) {
		password = MD5Utils.encrypt(username, password);
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			return "redirect:/index";
		} catch (AuthenticationException e) {
			System.out.println("登录失败信息------" + e.getMessage());
			return "redirect:/login";
		}
	}
	@Log("登录")
	@PostMapping("/login")
	@ResponseBody
	R ajaxLogin(String username, String password) {
		password = MD5Utils.encrypt(username, password);
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			return R.ok();
		} catch (AuthenticationException e) {
			return R.error("用户或密码错误");
		}
	}
	@GetMapping("/logout")
	String logout() {
		ShiroUtils.logout();
		return "redirect:/login";
	}

	@GetMapping("/main")
	String main(Model model) {
		//获取当前登录用户
		Subject subject = SecurityUtils.getSubject();
		SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
		//根据用户获取身份
		EduDO eduDO = eduService.getUserBySysUserId(sysUserDO.getUserId());
		if(sysUserDO.getUserId() == 1){  //管理员
			model.addAttribute("user", sysUserDO);
			model.addAttribute("isAdmin", 1);
		}else{
			if(eduDO.getSex().equals("1")){
				eduDO.setSex("男");
			}else{
				eduDO.setSex("女");
			}
			String value = eduDO.getUnit();
			if (value.equals("1")) {
				eduDO.setUnit("一年级");
			} else if (value.equals("2")){
				eduDO.setUnit("一年级");
			}else if (value.equals("3")){
				eduDO.setUnit("一年级");
			}else if (value.equals("4")){
				eduDO.setUnit("一年级");
			}else if (value.equals("5")){
				eduDO.setUnit("一年级");
			}else if (value.equals("6")){
				eduDO.setUnit("一年级");
			}
			model.addAttribute("user", eduDO);
			model.addAttribute("isAdmin", 0);
		}

		return "main";
	}
	@GetMapping("/403")
	String error403() {
		return "403";
	}

}
